package cst.a722wr.zyfypt_722wr.Fragment;


import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.List;

import cst.a722wr.zyfypt_722wr.Adapter.CProjectAdapter;
import cst.a722wr.zyfypt_722wr.Bean.CollectResult;
import cst.a722wr.zyfypt_722wr.Bean.ProjectBean;
import cst.a722wr.zyfypt_722wr.R;
import cst.a722wr.zyfypt_722wr.iface.CollectListListener;
import cst.a722wr.zyfypt_722wr.model.CollectListModel;


public class CProjectFragment extends Fragment {
    private View view;
    private Context context;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    private CProjectAdapter adapter;
    private List<CollectResult<ProjectBean>> list = null;
    private SharedPreferences sp;
    private String sessionID;
    private int page=1;
    private int lastVisibleItemPosition;

    CollectListListener<ProjectBean> listListener = new CollectListListener<ProjectBean>() {
        @Override
        public void onResponse(List<CollectResult<ProjectBean>> beanlist) {
            if(page == 1) {
                list = beanlist;
            } else {
                list.removeAll(beanlist);
                list.addAll(beanlist);
            }
            adapter.setList(list);
            adapter.notifyDataSetChanged();
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        view = inflater.inflate(R.layout.fragment_cproject, container, false);
        sp = getActivity().getSharedPreferences("ME", Context.MODE_PRIVATE);
        sessionID = sp.getString("session_id",null);

        System.out.println("----onCreateView page="+page);

        CollectListModel model=new CollectListModel();
        model.getResultList("project", page, sessionID, listListener);


        initRecyclerView();

        return view;
    }

    private void initRecyclerView() {
        recyclerView=(RecyclerView)view.findViewById(R.id.cproject_rec);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        adapter = new CProjectAdapter(context);
        adapter.setList(list);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    page += 1;
                    CollectListModel carticleModel = new CollectListModel();
                    carticleModel.getResultList("project", page, sessionID, listListener);
                    System.out.println("----onScrollStateChanged  page="+page);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=layoutManager.findLastVisibleItemPosition();
                //lastVisibleItemPosition = list.size() - 1;
            }
        });
    }

}
